<?php
/**
 * Created by PhpStorm.
 * User: sin
 * Date: 2019/2/21
 * Time: 3:05 PM
 */

namespace app\api\controller;

use app\common\enum\Source;
use app\common\ext\IRequest;
use app\common\util\StringUtil;
use think\Controller;
use think\Request;
use think\Session;

class Web extends Controller{

    public function index(){
        $token = IRequest::get('token');

        //取得控制器方法
        $controller = IRequest::get('_c',IRequest::NOT_EMPTY,'_c不能为空');
        $action = IRequest::get('_a',IRequest::NOT_EMPTY,'_a不能为空');

        $source = 'not_app';

        //设置全局变量
        define('CALL_SOURCE',$source);

        if(empty($token)){
            IRequest::set('device_no',md5('mini'.StringUtil::randString(32)));
            IRequest::set('os',$source);
            IRequest::set('version','1.0.0');
            Session::set("api_not_auth",true);
            $even = controller("api/system");
            $token_action = 'get_init_token';

            //判断方法是否存在
            if(!method_exists($even,$token_action)){
                $this->error("亲,你迷路了");
                return false;
            }

            $token = $even->$token_action();

            Session::set("api_not_auth",false);
        }

        //取得用ID
        $_POST = $_REQUEST; //微信请求都是GET类型
        $_POST['_t'] = $token;

        //实例化控制器
        $even = controller("api/{$controller}");

        //判断方法是否存在
        if(!method_exists($even,$action)){
            $this->error("亲,你迷路了");
            return false;
        }

        //调用方法名
        return $even->$action();
    }
}